!{include file='common/iheader.htm'}
<div id="i_nav">
	<h3 class="page_title">引用管理</h3>
	<a class="button" href="/manager.php?app=wiki">WIKI管理</a>
</div>
<form id="wiki_form" method="post" onsubmit="return false;">
	<div class="option_div">
		!{html_options name="word_type" options=$types class="change_type" selected=$smarty.request.type}
	</div>
	<ul class="list_view">
		!{foreach from=$recommend.items item=dict key=id}
		<li index="!{$id}">
			<strong><label><input type="checkbox" name="ids[]" value="!{$id}"> !{$dict.word}</label></strong>
			<div class="opt_div">
				<a href="/manager.php?app=wiki&act=edit_recommend&id=!{$id}&rid=!{$recommend._id}">编辑引用内容</a>
				!{if $dict.src_image}
				<a href="/manager.php?app=wiki&act=edit_recommend_image&id=!{$id}&rid=!{$recommend._id}">编辑图片</a>
				!{/if}
			</div>
		</li>
		!{foreachelse}
		<li>
			<strong>暂无记录...</strong>
		</li>
		!{/foreach}
	</ul>
	<div class="option_div">
		<input type="button" value="删除" class="button delete_btn"> 　　
		<strong>词条名称：</strong>
		<input type="text" name="word" class="text_input"/><input type="button" value="追加" class="button add_btn"/>
	</div>
</form>
<script>
	var sort_ids = '';
	$(function(){
		$('.change_type').change(function(){
			var type = $(this).val();
			window.location.href = '/manager.php?app=wiki&act=recommend&type=' + type;
		});
		
		sort_ids = init_sort();
		$(".list_view").sortable({
			'stop' : function(){
				if(init_sort() !== sort_ids){
					sort_ids = init_sort();
					$.post('/manager.php?app=wiki&act=sort',{
						'ids' : sort_ids,
						'type' : '!{$smarty.request.type}'
					},function(data){
						var json = eval('('+data+')');
						alert(json.info);
					});
				}
			}
		});
		
		$('.add_btn').click(function(){
			var word = $('input[name="word"]').val();
			if(word.length<=0){
				alert('词条名称不能为空！');
				$('input[name="word"]').focus();
				return;
			}
			$.post('/manager.php?app=wiki&act=add_recommend',{
				'word' : word,
				'type' : '!{$smarty.request.type}'
			},function(data){
				var json = eval('('+data+')');
				alert(json.info);
				if(json.result){
					window.location.reload();
				}
			});
		});
		
		$('.delete_btn').click(function(){
			if(!$('input[name="ids[]"]:checked').length){
				alert('未选择要删除的词条！');
				return;
			}
			if(confirm('是否继续删除选中词条的引用？')){
				var ids = [];
				$('input[name="ids[]"]:checked').each(function(){
					ids[ids.length] = $(this).val();
				});
				$.post('/manager.php?app=wiki&act=del_recommend',{
					ids : ids,
					type : !{$smarty.request.type}
				},function(data){
					var json = eval('('+data+')');
					alert(json.info);
					if(json.result){
						$('input[name="ids[]"]:checked').parents('li').remove();
					}
				});
			}
		});
	});
	
	
	function init_sort(){
		var ids = '';
		$(".list_view li").each(function(){
			ids += $(this).attr('index') + ';';
		});
		return ids;
	}
</script>
!{include file='common/ifooter.htm'}